﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Globalization;
using Config = System.Configuration.Configuration;

namespace ForumsBrowser.Common
{
    public static class Configuration
    {
        private static string ThreadUrlTemplate
        {
            get
            {
                return ConfigurationManager.AppSettings["ThreadUrlTemplate"];
            }
        }

        private static string ForumUrlTemplate
        {
            get
            {
                return ConfigurationManager.AppSettings["ForumUrlTemplate"];
            }
        }

        private static string ForumUrlSinceTemplate
        {
            get
            {
                return ConfigurationManager.AppSettings["ForumUrlSinceTemplate"];
            }
        }

        public static string DateTimeXmlFormat
        {
            get
            {
                return ConfigurationManager.AppSettings["DateTimeXmlFormat"];
            }
        }

        private static string AvailableForumUrlTemplate
        {
            get 
            { 
                return ConfigurationManager.AppSettings["AvailableForumsTemplate"];
            }
        }

        public static int WebRequestTimeout
        {
            get
            {
                return int.Parse(ConfigurationManager.AppSettings["WebRequestTimeoutSeconds"]) * 1000;
            }
        }

        private static string Get(string key)
        {
            Config config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (config.AppSettings.Settings[key] == null)
                return null;
            else
                return config.AppSettings.Settings[key].Value;
        }

        private static T Get<T>(string key, T defaultvalue) where T : IConvertible
        {
            try
            {
                return (T)Convert.ChangeType(Get(key), typeof(T));
            }
            catch
            {
                return defaultvalue;
            }
        }

        private static void Set(string key, string value)
        {
            if (value != null)
            {
                Config config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                config.AppSettings.Settings.Remove(key);
                config.AppSettings.Settings.Add(key, value.ToString());
                config.Save();
            }
        }

        public static bool IsMiniViewEnabled
        {
            get
            {
                return Get<bool>("IsMiniViewEnabled", true);
            }
            set
            {
                Set("IsMiniViewEnabled", value.ToString());
            }
        }

        public static int MiniViewMinHeight
        {
            get
            {
                return Get<int>("MiniViewMinHeight", 1);
            }
            set
            {
                Set("MiniViewMinHeight", value.ToString());
            }
        }

        public static int PopupDuration
        {
            get
            {
                return Get<int>("PopupDuration", 10);
            }
            set
            {
                Set("PopupDuration", value.ToString());
            }
        }

        public static int MaxPopupsCount
        {
            get
            {
                return Get<int>("MaxPopupsCount", 5);
            }
            set
            {
                Set("MaxPopupsCount", value.ToString());
            }
        }

        public static bool PopupsEnabled
        {
            get
            {
                return Get<bool>("PopupsEnabled", true);
            }
            set
            {
                Set("PopupsEnabled", value.ToString());
            }
        }

        public static CultureInfo Culture
        {
            get
            {
                Config config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                if (config.AppSettings.Settings["SelectedCulture"] == null)
                    return CultureInfo.CurrentCulture;
                else
                    return CultureInfo.GetCultureInfo(config.AppSettings.Settings["SelectedCulture"].Value);
            }
            set
            {
                Config config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                config.AppSettings.Settings.Remove("SelectedCulture");
                config.AppSettings.Settings.Add("SelectedCulture", value.Name);
                config.Save();
            }
        }

        public static string GetThreadUrl(string forumName, Guid threadId)
        {
            return string.Format(ThreadUrlTemplate, Configuration.Culture.Name, forumName, threadId);
        }

        public static string GetForumUrl(string forumName, DateTime? since)
        {
            string format = ForumUrlTemplate;
            string sinceString = string.Empty;
            if (since.HasValue)
            {
                if (since.Value.Kind != DateTimeKind.Utc)
                    since = DateTime.SpecifyKind(since.Value, DateTimeKind.Utc);
                sinceString = string.Format(ForumUrlSinceTemplate, since.Value);
            }
            return string.Format(ForumUrlTemplate, Configuration.Culture.Name, forumName) + sinceString;
        }

        public static string AvailableForumsUrl
        {
            get
            {
                return string.Format(AvailableForumUrlTemplate, Configuration.Culture.Name);
            }
        }
    }
}
